﻿using Forum.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Forum.Controllers
{
    public class AnswerController : Controller
    {
        [HttpGet]
        public ActionResult CreateAnswer(int id)
        {
            CreateAnswerModel answerModel = new CreateAnswerModel();
            answerModel.PostId = id;
            ForumBL.PostBL postContext = new ForumBL.PostBL();
            ForumDomainModels.Post post = new ForumDomainModels.Post();
            post.Id = id;
            post = postContext.GetPostById(post);
            answerModel.PostTitle = post.Title;
            return View(answerModel);
        }

        [HttpPost]
        public ActionResult CreateAnswer(CreateAnswerModel answerModel)
        {
            ForumDomainModels.Answer answer = new ForumDomainModels.Answer();
            answer.Content = answerModel.Content;
            answer.AuthorId = (int)HttpContext.Session["Id"];
            answer.DatePublished = DateTime.Now;
            answer.PostId = answerModel.PostId;
            ForumBL.AnswerBL answerContext = new ForumBL.AnswerBL();
            answerContext.InsertAnswer(answer);
            return RedirectToAction("PostDetail", "Post", new
            {
                id = answerModel.PostId
            });
        }

    }
}
